package com.testapp.bluetoothtest08.Judge;


import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.File.DeviceFile;
import com.testapp.bluetoothtest08.File.MD5;
import com.testapp.bluetoothtest08.File.ScanMachineFile;
import com.testapp.bluetoothtest08.File.Statement;
import com.testapp.bluetoothtest08.File.Token;
import com.testapp.bluetoothtest08.Http.OkHttp;
import com.testapp.bluetoothtest08.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IsYellow {

    private String localData = null;
    private String redKey = null;
    private String redT = null;
    static String redList[] = new String[14 * 12];
    private boolean flag = false;
    private boolean scanFlag = false;
    OkHttp http = new OkHttp();

    public IsYellow() {
        this.localData = new DeviceFile().readAllDevice();

    }

    public void judge() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //先看一下本地设备
                    System.out.println("本地设备： " + localData);
                    //向服务器获取红名单
                    String respData = http.httpGetWithNoToken(Constants.GET_RED_URL);
                    System.out.println("服务器返回 ----> " + respData);


                    if (respData != null) {
                        JSONObject o = new JSONObject(respData);
                        String data = o.getString("data");
                        //放入JsonArray
                        JSONArray array = new JSONArray(data);
                        System.out.println("接收到的JsonArray ----> " + array);
                        //遍历每个array
                        for (int i = 0; i < array.length(); i++) {
                            //放入json单个对象
                            JSONObject object = array.getJSONObject(i);
                            //获取t和key
                            redT = object.getString("t");
                            redKey = object.getString("key");
                            System.out.println("Array" + i + "获得的t ----> " + redT);
                            System.out.println("Array" + i + "获得的key ----> " + redKey);
                            //把t和key扩展
                            ListT(redT, redKey);
                            //遍历每个红编码
                            for (String s : redList) {
                                //判断本地是否包含
                                if (localData.contains(s)) {
                                    System.out.println("我是接触人员！");
                                    flag = true;
                                    String state = new Statement().read();
                                    if(!state.equals("确诊者")){
                                        new Statement().write("接触者");
                                        String result = http.httpGet(Constants.UPLOAD_YELLOW_URL);
                                        System.out.println("服务器返回结果 ----> " + result);
                                    }
                                    break;
                                }
                            }
                            if (flag) {
                                break;
                            }
                        }

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void ListT(String t, String key) {
        try {
            redList[0] = MD5.md5(t, key);
            for (int i = 1; i < redList.length; i++) {
                redList[i] = MD5.md5(redList[i - 1], key);
                System.out.println(redList[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void judgeScanMachine(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String respData = http.httpGetWithNoToken(Constants.MACHINE_DATA);
                if(respData!=null){
                    try {
                        JSONObject object = new JSONObject(respData);
                        System.out.println("sn服务器返回 --->"+respData);
                        String d = object.getString("data");
                        System.out.println("服务器data ---> "+d);
                        String dd[] = d.split(",");
                        //遍历服务器传来的数组
                        for (String serviceData:dd) {
                            //读组本地的数据
                            System.out.println("sn" +serviceData);
                            String localData =getScanLocalData();
                            System.out.println("localDataScanMachine --> "+localData);
                            String data[] =  localData.split(";");
                            //遍历每个数据
                            for (String datum : data) {
                                if(serviceData.contains(datum)){

                                    System.out.println("我是接触人员！");
                                    scanFlag = true;
                                    String state = new Statement().read();
                                    if(!state.equals("确诊者")){
                                        new Statement().write("接触者");
                                        String result = http.httpGet(Constants.UPLOAD_YELLOW_URL);
                                        System.out.println("服务器返回结果 ----> " + result);
                                    }
                                    break;
                                }
                            }

                            if(scanFlag==true){
                                break;
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();

    }

    public String getScanLocalData(){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 14; i++) {
            String tmp = new ScanMachineFile().readData(i);
            sb.append(tmp);
        }
        return sb.toString();
    }
}
